package com.code;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class JTableTest {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JTableTest window = new JTableTest();
					window.frame.setVisible(true);
//					TableModel dataModel = new DefaultTableModel() {
//				          public int getColumnCount() { return 10; }
//				          public int getRowCount() { return 10;}
//				          public Object getValueAt(int row, int col) { return new Integer(row*col); }
//				      };
//				      JTable table = new JTable(dataModel);
				      JTable table = new JTable(new MyTable());
				      table.setPreferredScrollableViewportSize(new Dimension(550, 30));
				      JScrollPane scrollpane = new JScrollPane(table);
				      window.frame.add(scrollpane);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public JTableTest() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
}
class MyTable extends AbstractTableModel{
	Object[][] p = {
	    {"阿呆", new Integer(66), new Integer(32), new Integer(98), new Boolean(false),new Boolean(false)},
	    {"阿瓜", new Integer(85), new Integer(69), new Integer(154), new Boolean(true),new Boolean(false)},
	};
	String[] n = {"姓名", "语文","数学","总分","及格","作弊"};
	Class[] typeArray = new Class[]{String.class, Integer.class, Integer.class, Integer.class, Boolean.class, Boolean.class};
	public int getColumnCount() {
	    return n.length;
	}
	public int getRowCount() {
	    return p.length;
	}
	public String getColumnName(int col) {
	    return n[col];
	}
	public Object getValueAt(int row, int col) {
	    return p[row][col];
	}
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        this.p[rowIndex][columnIndex] = aValue;
        this.fireTableCellUpdated(rowIndex, columnIndex);
    }
	
	public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex >= 4;
    }
	public Class getColumnClass(int c) {
	    return typeArray[c];
	}
	}
